$(document).ready(function(){
//默认进入时绘图
    $('#datepicker').val(getToday());
    $('#dateinfo').text(getToday());
    GoodsTotalIncome();   //分商品种类累计收入
    dailyTotalIncome(startDate='2020-04-01',endDate='2020-05-01');   //每日累计所有收入
    dailyGoodIncome(date='2020-04-01');  //每日分商品种类统计收入
    Goods3DIncome();
//当改变日期时重新绘制每日商品销售收入图
 $('#datepicker').on('change',function(){
      CurDate=$(this).val();
      $('#dateinfo').text(CurDate);
      dailyGoodIncome(CurDate);
})
$('#btn_before').click(function(){
      currdate=$('#datepicker').val();
      date= getDay(currdate,-1);
      firstDay=getFirstDay();
      if (date==firstDay){
         layerMsg(msg='已经到起始日期了！');
      }else{
          $('#datepicker').val(date);
          $('#dateinfo').text(date);
          dailyGoodIncome(date);
      }
})
$('#btn_after').click(function(){
      today = getToday();
      currdate=$('#datepicker').val();
      if(currdate==today)
      {
        layerMsg(msg='已经到最新日期了！');
      }else{
        date= getDay(currdate,1);
         $('#datepicker').val(date);
         $('#dateinfo').text(date);
         dailyGoodIncome(date);
      }
})
$('#btn_initial').click(function(){
        date=getFirstDay();
        $('#datepicker').val(date);
        $('#dateinfo').text(date);
        dailyGoodIncome(date);
    })

$('#btn_last').click(function(){
    today = getToday();
    $('#datepicker').val(today);
    $('#dateinfo').text(today);
    dailyGoodIncome(today);
})

//每日累计销售收入可视化图
$('#btn_1month').click(function(){
    today=getToday();
    today30=getDay(today,-30);
    console.log(today,today30);
    dailyTotalIncome(today30,today);
})

$('#btn_1season').click(function(){
    today=getToday();
    today90=getDay(today,-90);
    dailyTotalIncome(today90,today);
})
$('#btn_1hyear').click(function(){
    today=getToday();
    today180=getDay(today,-180);
    dailyTotalIncome(today180,today);
})
$('#btn_1year').click(function(){
    today=getToday();
    today365=getDay(today,-365);
    dailyTotalIncome(today365,today);
})

})

function layerMsg(msg){
 layer.open({
        type: 1
        ,offset: 'auto' //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
        ,id: 'layerDemo'+1 //防止重复弹出
        ,content: '<div style="padding: 20px 20px;">'+msg+'</div>'
        ,btn: '关闭'
        ,btnAlign: 'c' //按钮居中
        ,shade: 0 //不显示遮罩
        ,yes: function(){
          layer.closeAll();
        }
      });
}
function getFirstDay(){
    $.get('/getFirstDay',function(res){
       if(res.length==0) {
          layerMsg(msg='没有查到，数据异常');
       }else{
           date=res[0][0];
       }
       return date;
    })
}
function getToday(){
        var time = new Date();
        var day = ("0" + time.getDate()).slice(-2);
        var month = ("0" + (time.getMonth() + 1)).slice(-2);
        var today = time.getFullYear() + "-" + (month) + "-" + (day);
        return today;
}
function getDay(currdate,flag){
     var timestamp = new Date(currdate).getTime()/1000;
     var yesday = timestamp+flag*60*60*24
     var time=new Date(yesday*1000);
     var month=time.getMonth()+1;
     Month = month<10 ?('0'+ String(month)):String(month);
     var date=time.getDate();
     cDate = date<10 ?('0'+ String(date)):String(date);
     var date=String(time.getFullYear()) +'-'+Month+'-'+ cDate;
     return date;
}

//每日分商品种类销售收入图
function dailyGoodIncome(date){
var params={};
   params.date=date;
   $.post('GoodsMarketByDay',params,function(res){
//       console.log(res);
       if(res=='{}') {
          layerMsg(msg='当前日期没有数据，请重新选择日期');
       }else{
           var objRes = JSON.parse(res);
           var list=new Array();
           var income=new Array();
           var goods=new Array();
           for( i in objRes){
              income.push(Math.round(objRes[i]/10000));
              goods.push(i);
              list.push({"name":i,"value":objRes[i]/10000});
           }
           var data={
              title:"",
              label:"收入",
              x:  goods,
              y: income,
              list:list
           };
           var myChart = echarts.init(document.getElementById('dailyIncomeBar'));
           echarts_bar_option(myChart,data);
           var myChart = echarts.init(document.getElementById('dailyIncomePie'));
           echarts_pie_option(myChart,data);
       }
 })
}
// 柱状显示每日销售总收入
function dailyTotalIncome(startDate,endDate){
       var params={};
        params.startDate=startDate;
        params.endDate=endDate;
//         console.log(params);
       $.post('dailyIncome',params,function(res){
//            console.log(res);
           var objRes = JSON.parse(res);
           var date=new Array();
           var income=new Array();
           for(i in objRes){
              date.push(i);
              income.push(objRes[i]/100000);
       }
       var data={
         title:"",
          label:"收入",
          x:  date,
          y: income
       };
       //柱状图
       var myChart = echarts.init(document.getElementById('totalIncomeBar'));
       echarts_bar_option1(myChart,data);
     //3D图
//        var myChart = echarts.init(document.getElementById('totalIncome3D'));
//       echarts_3d_option(myChart);
       })
}

//柱状显示累计各商品收入
function GoodsTotalIncome(){
 $.get('goodsStat',function(res){
 var objRes = JSON.parse(res);
       var goods=new Array();
       var income=new Array();
       for(i in objRes){
          goods.push(i);
          income.push(objRes[i]);
       }
       var data={
          title:"",
          label:"收入",
          x:  goods,
          y: income

       };
       var myChart = echarts.init(document.getElementById('dailyTotalIncomeLine'));
       echarts_line_option(myChart,data);
 })
 $.get('goodsStatBar',function(res){
        var objRes = JSON.parse(res);
       var goods=new Array();
       var income=new Array();
       for(i in objRes){
          goods.push(i);
          income.push(objRes[i]/1000000);
       }
       var data={
          title:"",
          label:"收入",
          x:  goods,
          y: income

       };
      var myChart = echarts.init(document.getElementById('dailyTotalIncomeBar'));
       echarts_bar_option2(myChart,data);
  })
  }

function Goods3DIncome(){
    $.get('/get3DData',function(res){
//      console.log(res);
       var myChart = echarts.init(document.getElementById('totalIncome3D'));
       echarts_3d_option(myChart,res);
    })

}
//柱状显示模板
function echarts_bar_option(chartName,data){
  var option = {
    title: {
        text: data.title
    },
    tooltip: {},
    legend: {
        data:data.label
    },
    xAxis: {
        data:data.x,
        type:'category',
        axisLabel: {interval:0,rotate:45,fontSize:10,color: '#c3dbff'}
    },
    yAxis: {
       min:function(value){return Math.round(value.min-0.05*value.min)},
       max:function(value){return Math.round(value.max+0.05*value.max)},
       axisLabel: {fontSize:10,color: '#c3dbff',showMinLabel: false,
          showMaxLabel: false},
       minInterval:1
    },
    series: [{
        name: '收入',
        type: 'bar',
        data: data.y,
        itemStyle:{
            normal:{
                color: function(parmas){
                  var index_color = parmas.value;
                  var index_max = parmas.value.min;
                  var index_min = parmas.value.max;
                  if (index_color)
                   var colorList=[ '#e40c5a', '#e40ca0', '#9d0ce4', '#570ce4',
                    '#0c1be4', '#0c61e4', '#0ce2e4', '#0ce496','#0ce450','#49e40c','#22e40c','#85e40c','#e4df0c',
                    '#e4a30c', '#e4710c', '#e45d0c','#e40c0c'];
                  return colorList[parmas.dataIndex%colorList.length];
                }
            }
        }
    }]
    };
  chartName.setOption(option);
  }
function echarts_bar_option1(chartName,data){
  var option = {
    title: {
        text: data.title
    },

    tooltip: {},
    legend: {
        data:data.label
    },
    xAxis: {
        data:data.x,
        type:'category',
        axisLabel: {fontSize:10,color: '#c3dbff'}
    },
    yAxis: {
       min:function(value){return Math.round(value.min-0.05*value.min)},
       max:function(value){return Math.round(value.max+0.05*value.max)},
       axisLabel: {fontSize:10,color: '#c3dbff'},
       minInterval:1
    },
    series: [{
        name: '收入',
        type: 'bar',
        data: data.y,
        itemStyle:{
            normal:{
                color: function(parmas){
                  var index_color = parmas.value;
                  var index_max = parmas.value.min;
                  var index_min = parmas.value.max;
                  if (index_color)
                  var colorList=[ '#ff7e50', '#97d3f9', '#dd70d9', '#34cf34',
                    '#6497ef', '#85802b', '#D7504B', '#C6E579',
                    '#F4E001', '#F0805A', '#26C0C0'];
                  return colorList[parmas.dataIndex%colorList.length];
                }
            }
        }
    }]
    };
  chartName.setOption(option);
  }
function echarts_bar_option2(chartName,data){
  var option = {
    title: {
        text: data.title
    },

    tooltip: {},
    legend: {
        data:data.label
    },
    xAxis: {
        min:function(value){return Math.round(value.min-0.01*value.min)},
        max:function(value){return Math.round(value.max+0.01*value.max)},
         axisLabel: {fontSize:10,color: '#c3dbff'},
    },
    yAxis: {
        data:data.x,
        type:'category',
        axisLabel: {interval:0,fontSize:10,color: '#c3dbff'}
    },
    series: [{
        name: '收入',
        type: 'bar',
        data: data.y,
        itemStyle:{
            normal:{
                color: function(parmas){
                  var index_color = parmas.value;
                  var index_max = parmas.value.min;
                  var index_min = parmas.value.max;
                  if (index_color)
                  var colorList=[ '#e40c0c', '#e45d0c', '#e4710c', '#e4a30c','#e4df0c',,'#e4df0c','#22e40c',
                  ,'#49e40c','#0ce450','#0ce2e4','#0c61e4','#0c1be4','#570ce4','#9d0ce4','#e40ca0','#e40c5a'];
                  return colorList[parmas.dataIndex%colorList.length];
                }
            }
        }
    }]
    };
  chartName.setOption(option);
  }
function echarts_line_option(chartName,data){
  var option = {
    title: {
        text: data.title
    },

    tooltip: {},
    legend: {
        data:data.label
    },
    xAxis: {
        data:data.x,
        type:'category',
        axisLabel: {interval:0,rotate:-30,fontSize:10,color: '#c3dbff'}
    },
    yAxis: {
       min:function(value){return Math.round(value.min-0.05*value.min)},
       max:function(value){return Math.round(value.max+0.05*value.max)},
       axisLabel: {fontSize:10,color: '#c3dbff'},
       minInterval:1
    },
    series: [{
        name: '收入',
        type: 'line',
        data: data.y,
        itemStyle:{
            normal:{
                color: function(parmas){
                  var index_color = parmas.value;
                  var index_max = parmas.value.min;
                  var index_min = parmas.value.max;
                  if (index_color)
                  var colorList=[ '#e40c5a', '#e40ca0', '#9d0ce4', '#570ce4',
                    '#0c1be4', '#0c61e4', '#0ce2e4', '#0ce496','#0ce450','#49e40c','#22e40c','#85e40c','#e4df0c',
                    '#e4a30c', '#e4710c', '#e45d0c','#e40c0c'];
                  return colorList[parmas.dataIndex%colorList.length];
                }
            }
        }
    }]
    };
  chartName.setOption(option);
  }
function echarts_pie_option(chartName,data){
  var option={
     color:[ '#e40c5a', '#e40ca0', '#9d0ce4', '#570ce4',
                    '#0c1be4', '#0c61e4', '#0ce2e4', '#0ce496','#0ce450','#49e40c','#22e40c','#85e40c','#e4df0c',
                    '#e4a30c', '#e4710c', '#e45d0c','#e40c0c'],
     series:[
        {
          name:'',
          type:'pie',
          radius:'80%',
          roseType:'angle',
          data:data.list
        }
     ]
  }
  chartName.setOption(option);
}
function echarts_3d_option(chartName,data){
  var option={
       title:{
            text: ''
        },
        xAxis3D:{
            type: 'category',
            axisLabel: {
                show: true,
                textStyle: {
                  color: '#fff',
                  fontSize:10
                }
        }},
        yAxis3D:{
           type: 'category',
           axisLabel: {
                show: true,
                textStyle: {
                  color: '#fff',
                  fontSize:10
                }
        }},
        zAxis3D:{
           type: 'value',
           axisLabel: {
                show: true,
                textStyle: {
                  color: '#fff',
                  fontSize:10
                }
        }},
        visualMap: {
                  show: false,
                  min: 500000,
                  max: 1500000,
                  inRange: {
                      color: ["#0c57e4", "#0cbae4", "#0ce478","#71e40c", "#e4d50c", "#e4b70c","#e4710c","#e4530c","#e40c0c"]
                },
                 calculable:true
             },
        grid3D:{},
        series:[
          {
             type:'bar3D',
             data: data ,
             lineStyle:{
               width:2
             }
          }
         ]

  }
  chartName.setOption(option);
}